home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 9 / Night Owl CD-ROM (NOPV9) (Night Owl Publisher) (1993).ISO / 009a / snpd0493.zip / ISWPROT.C < prev    next >
C/C++ Source or Header  |  1993-04-05  |  2KB  |  78 lines

  1. /*
  2. **  ISWPROT.C - Detect if floppy drive is write protected
  3. **
  4. **  public domain by Bob Stout
  5. */
  6.  
  7. #include <dos.h>
  8.  
  9. #ifdef __TURBOC__
  10.  #define FAR far
  11. #else
  12.  #define FAR _far
  13. #endif
  14.  
  15. /*
  16. **  isWprot()
  17. **
  18. **  Parameters: 1 - Drive number (A: = 0, B: = 1)
  19. **
  20. **  Returns: -1 - Error
  21. **            0 - Not write protected
  22. **            1 write protected
  23. **
  24. **  Note: If drive door is open, an error is returned but the critical
  25. **        error handler is NOT tripped
  26. */
  27.  
  28. int isWprot(int drive)
  29. {
  30.       union REGS regs;
  31.       struct SREGS sregs;
  32.       char buf[512], FAR *bufptr = (char FAR *)buf;   /* Needed by MSC  */
  33.  
  34.       /* First read sector 0  */
  35.  
  36.       segread(&sregs);
  37.       regs.x.ax = 0x201;
  38.       regs.x.cx = 1;
  39.       regs.x.dx = drive & 0x7f;
  40.       sregs.es  = FP_SEG(bufptr);
  41.       regs.x.bx = FP_OFF(bufptr);
  42.       int86x(0x13, ®s, ®s, &sregs);
  43.       if (regs.x.cflag && regs.h.ah != 6)
  44.             return -1;
  45.  
  46.       /* Try to write it back */
  47.  
  48.       segread(&sregs);
  49.       regs.x.ax = 0x301;
  50.       regs.x.cx = 1;
  51.       regs.x.dx = drive & 0x7f;
  52.       sregs.es  = FP_SEG(bufptr);
  53.       regs.x.bx = FP_OFF(bufptr);
  54.       int86x(0x13, ®s, ®s, &sregs);
  55.       return (3 == regs.h.ah);
  56. }
  57.  
  58. #ifdef TEST
  59.  
  60. #include <stdio.h>
  61. #include <ctype.h>
  62.  
  63. int main(int argc, char *argv[])
  64. {
  65.       int drive;
  66.  
  67.       if (2 > argc)
  68.       {
  69.             puts("Usage: ISWPROT drive_letter");
  70.             return -1;
  71.       }
  72.       drive = toupper(argv[1][0]) - 'A';
  73.       printf("isWprot(%c) returned %d\n", drive, isWprot(drive));
  74.       return 0;
  75. }
  76.  
  77. #endif
  78.